<template>
  <div class="wrapper" ref="wrapper">
    <div class="content">
      <slot></slot>
    </div>
  </div>
</template>

<script>
  import BScroll from 'better-scroll'

  export default {
    name: "Scroll",
    props: {
      probe: {
        type: Number,
        default: 0
      },
      pullUp: {
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        scroll: null
      }
    },
    mounted() {
      this.scroll = new BScroll(this.$refs.wrapper, {
        probeType: this.probe,
        click: true,
        pullUpLoad: this.pullUp,
        // pullUpLoad: {
        //   threshold: 20 // 在上拉到超过底部 20px 时，触发 pullingUp 事件
        // }
      })

      // 监听滚动的位置
      this.scroll.on('scroll', (position) => {
        // 谁用向谁发送，向外发送position滚动数据
        this.$emit('scroll', position)
      })

      this.scroll.on('pullingUp', () => {
        this.$emit('getMore')
      })
    },
    methods: {
      backTop(x, y, time = 100) {
        this.scroll && this.scroll.scrollTo(x, y, time)
      },
      scrollTo(x, y, time = 300) {
        this.scroll && this.scroll.scrollTo(x, y, time)
      },
      finishPullUp() {
        this.scroll && this.scroll.finishPullUp();
      },
      refresh() {
        this.scroll && this.scroll.refresh()
      }
    },
    beforeDestroy() {
      this.scroll.destroy()
    }
  }
</script>

<style lang="less" scoped>

</style>
